package com.wolf.etl.vo;


import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.*;

/**
 * Created by tommy's father on 2017/8/20.
 */
//数据库
public class Database implements Serializable{

    // 类别名称；它必须与存储在数据库中的类别名称匹配；该参数为 "" 表示获取没有类别的那些描述；为 null 则表示该类别名称不应该用于缩小搜索范围
    private String catalog;
    // schemaPattern - 模式名称的模式；它必须与存储在数据库中的模式名称匹配；该参数为 "" 表示获取没有模式的那些描述；为 null
    // 则表示该模式名称不应该用于缩小搜索范围
    private String schema;
    // 表格的集合
    private final Map<String, Table> tables = new HashMap<String, Table>();

    //构造方法
    public Database(String catalogFilter, String schemaFilter) {
        this.catalog = catalogFilter;
        this.schema = schemaFilter;
    }

    //getter/setter方法
    public String getCatalog() {
        return catalog;
    }

    public void setCatalog(String catalog) {
        this.catalog = catalog;
    }

    public String getSchema() {
        return schema;
    }

    public Map<String, Table> getTables() {
        return tables;
    }

    public void addTable(Table table) {
        tables.put(table.getName(), table);
    }

    public String[] getTableNames() {
        List<String> lst = new ArrayList<String>();
        Iterator<Map.Entry<String, Table>> iterator = tables.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Table> entry = iterator.next();
            lst.add(entry.getValue().getName());
        }
        if (CollectionUtils.isEmpty(lst)) {
            return null;
        }
        return lst.toArray(new String[lst.size()]);
    }

    public Table getTable(String tabName) {
        if (StringUtils.isEmpty(tabName)) {
            return null;
        }
        tabName = tabName.trim();


        Iterator<Map.Entry<String, Table>> iterator = tables.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Table> entry = iterator.next();
            if (entry.getKey().equalsIgnoreCase(tabName)) {
                return entry.getValue();
            }
        }
        return null;
    }
}
